/* cPr cParametros, cPa cParent, cEl cElement, w width,h height,  cTi cTitulo*/
var gbRichText;
var gnPopup = 0;var gCw = 600; gCh = 400;
var gbChange = gbSending= 0;
var gcElement = '';
var gcTitle = '';
var gcUrl;
var goXHR;
var gCloseFunction= closePopup;

function newDiv( i, txt, w, h, l, t, c, p) { 
	var o = id(i);
	if ( !i || !o ){
		o = document.createElement('div'); 
		o.id = i; 
	} else
		o.style.visibility = 'visible';
	if ( p ) p.appendChild( o ); 
	else document.body.appendChild( o ); 
	o.innerHTML = txt; 	
	if ( w ) o.style.width = w + "px"; 
	if ( h ) o.style.height = h + "px"; 
	if ( l ) o.style.left = l + "px";
	if ( t ) o.style.top = t + "px";
	if ( c ) o.className = c; 
	return o;
}
function id( cEl ) { return cEl ? document.getElementById( cEl ) : null ; }
function closeDivPopup()
{
	//hideWindow('div_popup');
	destroyWindow('div_popup' + (gnPopup));
	--gnPopup;
}
function closePopup(nPopup)
{
	//hideWindow( "div_popup" );
	if (nPopup == undefined ) nPopup = gnPopup;
	destroyWindow('div_popup' + nPopup);
	--gnPopup;
	gcWinId = 'div_popup' + gnPopup;
}
function refreshPopup()
{
	if (goXHR.readyState==4)
	{
		setSending ( 0 );
		t = goXHR.responseText;
		if ( t  )
		{
			// Si Recibe un Mensaje tipo "[Elemento]Mensaje[/Elemento]" => Modifica el Elemento
				while ( t.charAt( 0 ) == '[' )
				{
					nPos = t.indexOf( ']' );
					if ( nPos != -1 )
					{
						cElM = t.substring(1,nPos);
						nPosH = t.indexOf( '[/' + cElM + ']', nPos );
						if ( nPosH != -1 )
						{
							cHtmlM = t.substring(nPos + 1 , nPosH );
							if ( cElM == 'javascript' )
								eval(cHtmlM);
							else
							{
								o = id( cElM );
								if ( o )
									o.innerHTML = cHtmlM;
								else
									bOk = 1;
							}
							t = t.substring( nPosH + 3 + cElM.length );
							if ( t  == '' ) return 1;
						}
					} else break;
				}
			if ( !gbChange ) ++gnPopup;
			createWindow( 'div_popup' + gnPopup, gcTitle , t , gCw, gCh , null, null, 1, 'closePopup('+gnPopup+')' ) ;
			jsInAjax('div_popup' + gnPopup);
		}
	}
}
function jsInAjax(El)
{
	var d = document.getElementById(El).getElementsByTagName("script");
	var t = d.length;
	for (var x=0;x<t;x++){
	var newScript = document.createElement('script');
	newScript.type = "text/javascript";
	newScript.text = d[x].text;
	document.getElementById(El).appendChild (newScript);
	}
	//if ( t > 0 ) if ( window.onload ) window.onload();
}
function openPopup(cUrl, n , w, h, c)
{
	if ( w ) gCw  = w;
	if ( h ) gCh  = h;
	if ( n ) gcTitle = n;
	gbChange = c ? c : 0;
	send( cUrl , refreshPopup, 'div_popup', 1, 0);
}
function refreshTarget( cTarget )
{
	o = id( cTarget );
	if ( o )
	{
		cUrl = o.getAttribute( "url" );
		cEl = gcElement;
		if ( cUrl  ) send( cUrl+'&bRefreshTarget=1', refreshElementSimple , cTarget , 0, 0 );
		gcElement = cEl;
	}
}
function send( cUrl , f , cEl , bAsync , bSetUrl, cTi )
{
	if ( bAsync == undefined ) bAsync = 1;
	// Si ya esta haciendo un send asincronico => no hace nada
	if ( bAsync && gbSending ) return 0;
	if ( bAsync ) setSending ( 1 );
	goXHR =createXMLHttpRequest();
	//while ( gcElement != '' && goXHR.readyState != 0 ) ;
	if ( cEl ) { gcElement = cEl; }
	if ( bSetUrl == undefined ) bSetUrl = 1;
	var cBody = null;
	if ( cUrl && bSetUrl) { gcUrl = cUrl; }
	if ( window.dhtmlHistory != undefined && cEl == 'body_main' ) 		dhtmlHistory.add(cTi, cUrl.substring(cUrl.indexOf( '?' )));
	goXHR.open("GET", cUrl, bAsync );
	if ( bAsync == 1 ) goXHR.onreadystatechange= f;
	//goXHR.setRequestHeader('charset', 'utf-8');
	goXHR.send( cBody );
	if ( bAsync == 0 )
		f( 0 );
	return 0;
}
function sendForm( cUrl, cFormName, f, cEl, bAsync )
{
	if ( bAsync == undefined ) bAsync = 1;
	goXHR =createXMLHttpRequest();
	//while ( gcElement != '' && goXHR.readyState != 0 ) ;
	gcElement = cEl;
	oForm = id( cFormName );
	if ( !oForm ) return 0;
	if ( bAsync ) setSending ( 1 );
	cBody = '';
	cContentType =   'application/x-www-form-urlencoded' ;
	if ( gbRichText )
		updateRTEs();
	//Arma cBody con las Variables del Form
	for ( var i = 0; i < oForm.length; i++ )
	{
		if ( oForm[i].id == 'hdn' + oForm[i].name )
			updateRTE(  oForm[i].name );
		if ( oForm[i].name && (oForm[i].type != 'checkbox' && oForm[i].type != 'radio' ) || ((oForm[i].type == 'checkbox' || oForm[i].type == 'radio') &&  oForm[i].checked ) )
			cBody +=  '&' + oForm[i].name + '=' + encodeURIComponent(oForm[i].value);
	}
	if ( cBody == '' ) cBody = null;
	else cBody  = cBody.substring(1);
	goXHR.open("POST", cUrl, bAsync );

	goXHR.setRequestHeader('Content-Type', cContentType );
	goXHR.setRequestHeader('charset', 'utf-8');
	if ( bAsync == 1 ) goXHR.onreadystatechange= f;
	goXHR.send( cBody );
	if ( bAsync == 0 )
		f( 0 );
	return 0;
}
function createXMLHttpRequest()
{
	try {
		goXHR = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e)
		{
			try {
				goXHR = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E)
			{
				goXHR = 0;
			}
	}
	if (!goXHR && typeof XMLHttpRequest!='undefined')
		goXHR = new XMLHttpRequest();
	return goXHR;
}
function setSending ( b )
{
	gbSending = b;
	o= newDiv( 'sending' , '<img src="imagenes/cargando.gif" alt="Espere un momento" />' ); 
	if ( b )
		o.style.zIndex = "99";		
	else
		o.style.visibility = "hidden";
}
function showHideElement(cEl, showHide) {
// function to show or hide elements. Element variable can be string or object
	if (id(cEl)) {
		o = id(cEl);
	} else return 0;
	if ( showHide == undefined )
		showHide = o.style.visibility == "hidden" ? "show": "hide";
	if (showHide == "show") 
		o.style.visibility = "visible";
	else if (showHide == "hide") 
		o.style.visibility = "hidden";
}
function isExplorer() {
	return (navigator.appName == 'Microsoft Internet Explorer') ? 1: 0 ;
}
function openWindow(cUrl, cNombre)
{
	cPr = 'height=' + parseInt(window.screen.height) * 0.7 ;
	cPr += ',width=' + parseInt(window.screen.width) * 0.7;
	cPr += (isExplorer() ? ',top=' :',screenX=' )+ parseInt(window.screen.height) * 0.15;
	cPr += (isExplorer() ? ',left=': ',screenY='  ) + parseInt(window.screen.width) * 0.15;
	cPr += ',scrollbars=yes,resizable=yes,status=no';
	o = window.open(cUrl, cNombre, cPr);
	o.focus();
	return 1;
}
function back( cUrl ){
	if( !window.opener) 
		window.location.href = cUrl;
	else if ( window.history.length == 1 ) window.history.go(-1);
}
////////////////////////////////
// Funciones para manejar el que ejecute una accion dado un delay de inactividad
////////////////////////////////
var gcActionDelayedElement = null;
function actionDelayedIn( o , cAction , cTimeInterval )
{
	if ( cTimeInterval == undefined) cTimeInterval  = 1000
	gcActionDelayedElement =  cEl = o.getAttribute('id');
	if ( cEl )
		setTimeout( 'if ( gcActionDelayedElement == "' + cEl + '" )' + cAction + ' ("' + cEl + '"); ', cTimeInterval );
}
function actionDelayedOut( o, cAction  )
{
	cEl = o.getAttribute('id');
	if ( gcActionDelayedElement == cEl && cAction ) setTimeout( cAction + ' ("' + cEl + '");  ', 1000 );
	gcActionDelayedElement = null;
}
function actionDelayedHide()
{
	hideWindow( 'div_context') ;
}
function actionDelayedSend( cEl)
{
	o = id( cEl );
	if ( o )
	{
		cUrl = o.getAttribute('url');
		if ( cUrl ) send(cUrl, refreshContextPopup, 'Popup' );
	}
}
function refreshContextPopup()
{
	if (goXHR.readyState==4)
	{
		setSending ( 0 );
		t = goXHR.responseText;
		createWindow( 'div_context', gcElement , t , 400, 200, null, null, 0) ;
	}
}
function refreshElementSimple()
{
	if (goXHR.readyState==4)
	{
		setSending ( 0 );
		t = goXHR.responseText;
		if ( t  )
		{
			o = id( gcElement );
			if ( o )
			{
				o.innerHTML = t;
				jsInAjax(gcElement);
			}
		}
	}
}
function refreshAppendElement()
{
	if (goXHR.readyState==4)
	{
		setSending ( 0 );
		t = goXHR.responseText;
		if ( t  )
		{
			o = id( gcElement );
			if ( o )
			{
				o.innerHTML += t;
				jsInAjax(gcElement);
			}
		}
	}
}
function refreshElement()
{
	if (goXHR.readyState==4)
	{
		var bOk = 0;
		setSending ( 0 );
		var t = goXHR.responseText;
		if ( t  )
		{
			// Si Recibe un Mensaje tipo "[Elemento]Mensaje[/Elemento]" => Modifica el Elemento
			while ( t.charAt( 0 ) == '[' )
			{
				nPos = t.indexOf( ']' );
				if ( nPos != -1 )
				{
					cElM = t.substring(1,nPos);
					nPosH = t.indexOf( '[/' + cElM + ']', nPos );
					if ( nPosH != -1 )
					{
						cHtmlM = t.substring(nPos + 1 , nPosH );
						if ( cElM == 'javascript' )
							eval(cHtmlM);
						else
						{
							o = id( cElM );
							if ( o )
							{
								o.innerHTML = cHtmlM;
								jsInAjax(cElM);
							} else //alert ( 'El elemento ' + cElM + ' no existe ' );
								bOk = 1;
						}
						t = t.substring( nPosH + 3 + cElM.length );
						if ( t  == '' ) return 1;
					}
				} else break;
			}
			if ( gcElement == 'javascript' )
				eval(t);
			else if ( gcElement != '' )
			{
				o = id( gcElement );
				if ( o )
				{
					if ( gcUrl )
					{
						o.setAttribute( "url",gcUrl );
						gcUrl = "";
					}
					o.innerHTML = t;
					jsInAjax(gcElement);
					bOk = 1;
				}
			}
		}
		// Especial para el Mapa y photogaleria
		gcPhotoGallery = '';
	}
	return bOk;
}
function openElement( cEl, x, y , cPa, h, w )
{
	o = id( cEl );
	if ( h == undefined ) h = o.clientHeight;
	if ( w == undefined ) w = o.clientWidth;
	if ( cPa == undefined ) p = o.parent;
	else p = id( cPa );
	sh = p.clientHeight;
	sw = p.clientWidth;
	if ( (x+w) > sw ) x = x - w;
	if ( (y+h) > sh ) y = y - h;
	o.style.left = x + 'px';
	o.style.top = y + 'px';
	o.style.visibility = "visible"; //z-index = 0;
}
function closeElement( cEl )
	{
		o = id( cEl );
		o.style.visibility = "hidden";
	}
function mergeParameterUrl( cEl, cPr, cTi )
{
	o = id( cEl );
	if ( o )
	{
		cNewUrl = cUrl = o.getAttribute( "url" );
		if ( cUrl )
		{
			aParameters = cPr.split( '&' );
			for( i = 0; i < aParameters.length  ; i ++)
			{
				aParameter = aParameters[i].split( '=' );
				nPos = cNewUrl.indexOf( aParameter[0] + '=' );
				if ( nPos != -1 )
				{
					nPosHasta = cNewUrl.indexOf( '&',  nPos +  aParameter[0].length );
					cNewUrl = cNewUrl.substring( 0, nPos +  aParameter[0].length ) + '=' + aParameter[1] +  ( nPosHasta != -1  ? cNewUrl.substring( nPosHasta ): 0) ;
				} else
					cNewUrl += '&' + aParameter[0] + '=' + aParameter[1];
			}
			o.setAttribute( "url",cNewUrl );
			//if ( dhtmlHistory && cEl == 'body_main' )				dhtmlHistory.add(cTi, cNewUrl.substring(cNewUrl.indexOf( '?' )));
		}
	}
	return 0;
}